package me.date.a03test;

public class Test3 {
    public static void main(String[] args) {
        //定义方法实现toBinaryString方法的效果
        //十进制转二进制，字符串的形式
        System.out.println(tobinarystring(6));
    }

    public static String tobinarystring(int num) {
        //逻辑：不断的除以2，倒着拼接余数
        //用StringBuilder
        StringBuilder sb = new StringBuilder();
        while (true) {
            if (num == 0) {
                break;
            }
            //先获取余数
            int remaindar = num % 2;
            //倒着拼接
            sb.insert(0, remaindar);
            num = num / 2;
        }
        return sb.toString();
    }
}
